import BaseHistory from "./base";

// 代表 html5 model 继承于 BaseHistory
export default class Html5History extends BaseHistory {
  constructor(options) {
    super(options);

    this.initListener(); // 处理事件监听
  }

  initListener() {
    window.addEventListener("popstate", () => {
      this.transitionTo(this.getCurrentLocation()); // 跳转对应的路由
    });
  }

  // 两种model 获取路由不同 
  getCurrentLocation() {
    let path = decodeURI(window.location.pathname) || "/";
    return path + window.location.search + window.location.hash;
  }

  push(target) {
    this.transitionTo(target); // 需要手动将逻辑 transitionTo 指定的路由
    window.history.pushState({ key: +new Date() }, "", target); // 向popstate 添加一条记录
  }
}
